<template>
  <div class="product-container">
    <!-- 顶部导航栏 -->
   <!-- 修改后的顶部导航栏 -->
   <nav class="top-nav" :class="{ 'nav-hidden': isNavHidden }">
    <div class="nav-content">
      <div class="logo" @click="router.push('/')">
        <div class="logo-glow"></div>
        <img 
          src="https://cdn-icons-png.flaticon.com/512/3063/3063795.png" 
          class="logo-icon"
          alt="校园二手交易"
        />
        <h1>
          <span class="logo-text">校园二手交易</span>
          <span class="logo-sub">Campus Marketplace</span>
        </h1>
      </div>
      
      <div class="nav-links">
        <router-link 
          v-for="(nav, index) in navItems" 
          :key="index" 
          to="center"
          class="nav-item"
          :class="{ 'active': $route.path === nav.path }"
          @mouseenter="hoverNav(index)"
          @mouseleave="hoverNav(null)"
        >
          <div class="nav-bg"></div>
          <i :class="['iconfont', nav.icon]"></i>
          <span class="nav-label">{{ nav.label }}</span>
          <div class="nav-underline"></div>
        </router-link>
      </div>

      <!-- 移动端汉堡菜单 -->
      <div class="hamburger" @click="isMobileMenuOpen = !isMobileMenuOpen">
        <div class="hamburger-line" :class="{ 'open': isMobileMenuOpen }"></div>
      </div>
    </div>

    <!-- 移动端菜单 -->
    <transition name="mobile-menu">
      <div v-show="isMobileMenuOpen" class="mobile-nav">
        <router-link
          v-for="(nav, index) in navItems"
          :key="index"
          :to="nav.path"
          class="mobile-nav-item"
          @click="isMobileMenuOpen = false"
        >
          {{ nav.label }}
        </router-link>
      </div>
    </transition>

    <!-- 导航栏底部流光 -->
    <div class="nav-shine"></div>
  </nav>

    <!-- 主要内容区域 -->
    <main class="main-content">
      <!-- 分类和搜索区域 -->
      <div class="header">
        <div class="nav-menu">
          <div class="menu-item active">
            <i class="iconfont icon-all"></i>
            <span>全部</span>
          </div>
          <div class="menu-item" v-for="(category, index) in categories" :key="index">
            <i :class="['iconfont', category.icon]"></i>
            <span>{{ category.name }}</span>
          </div>
        </div>
        <div class="search-box">
          <!-- 绑定输入框内容到 searchKeyword 并监听输入事件 -->
          <input type="text" v-model="searchKeyword"  placeholder="搜索你想要的商品" />
          <button class="search-btn" @click="getGoodsa">
            <i class="search-icon">🔍</i>
          </button>
        </div>
      </div>

      <!-- 新增轮播图区域 -->
      <el-carousel :interval="4000" type="card" height="300px">
        <el-carousel-item v-for="(item, index) in carouselImages" :key="index">
          <img :src="item.imageUrl" alt="轮播图" class="carousel-image" />
        </el-carousel-item>
      </el-carousel>

      <!-- 商品列表区域 -->
      <div class="product-list">
        <div class="product-item" v-for="(item, index) in products" :key="index">
          <div class="product-tag" v-if="item.isSpecial">限时特惠</div>
          <div class="product-image-wrapper">
            <img :src="item.coverImage" :alt="item.goodsName" class="product-image" />
            <div class="product-hover">
              <button class="quick-view" @click="jumpDetail(item.goodsId)">查看详情</button>
           
            </div>
          </div>
          <div class="product-info">
            <h3 class="product-name">{{ item.goodsName }}</h3>
            <p class="product-desc">{{ item.introduction }}</p>
            <div class="product-meta">
              <div class="product-price">
                <span class="price-symbol">¥</span>
                <span class="price-value">{{ item.price }}</span>
              </div>
              <div class="product-stats">
                <span class="product-sales">已售 {{ item.stock }}</span>
                <span class="product-sales" style=" color: green;
  background-color: #e8f5e9;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;">包邮</span>
                <span class="product-rating">
                  <i class="iconfont icon-star"></i>
                  4.9
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<style scoped>
.carousel-container {
  position: relative;
  width: 100%;
  overflow: hidden;
  margin: 0 auto;
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.carousel-outer-wrapper {
  overflow: hidden;
}

.carousel-inner-wrapper {
  display: flex;
  transition: transform 0.5s ease;
  will-change: transform;
}

.carousel-item {
  flex-shrink: 0;
  perspective: 1000px;
}

.carousel-card {
  position: relative;
  width: 100%;
  height: 300px;
  border-radius: 12px;
  overflow: hidden;
  transform: scale(0.95);
  transition: all 0.5s ease;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.carousel-card.active-card {
  transform: scale(1);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  z-index: 10;
}

.card-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to top, rgba(0,0,0,0.7), transparent);
  transition: opacity 0.5s ease;
}

.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.8s ease;
}

.carousel-card:hover .carousel-image {
  transform: scale(1.05);
}

.carousel-content {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 25px;
  color: white;
  z-index: 20;
  transition: all 0.5s ease;
}

.carousel-title {
  margin: 0 0 8px 0;
  font-size: 22px;
  font-weight: 600;
  transform: translateY(20px);
  opacity: 0;
  transition: all 0.5s ease 0.1s;
}

.carousel-desc {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
  transform: translateY(20px);
  opacity: 0;
  transition: all 0.5s ease 0.2s;
}

.carousel-card.active-card .carousel-title,
.carousel-card.active-card .carousel-desc {
  transform: translateY(0);
  opacity: 1;
}

.carousel-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 50%;
  border: none;
  cursor: pointer;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  opacity: 0;
  visibility: hidden;
}

.carousel-container:hover .carousel-control {
  opacity: 1;
  visibility: visible;
}

.carousel-control:hover {
  background: white;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
}

.carousel-control.prev {
  left: 15px;
}

.carousel-control.next {
  right: 15px;
}

.carousel-control:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.carousel-control i {
  font-size: 20px;
  color: #333;
}

.carousel-indicators {
  position: absolute;
  bottom: 15px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 8px;
  z-index: 30;
}

.carousel-indicators button {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.6);
  cursor: pointer;
  transition: all 0.3s ease;
}

.carousel-indicators button.active {
  width: 30px;
  border-radius: 5px;
  background: white;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .carousel-content {
    padding: 15px;
  }
  
  .carousel-title {
    font-size: 18px;
  }
  
  .carousel-desc {
    font-size: 12px;
  }
  
  .carousel-control {
    opacity: 1;
    visibility: visible;
    width: 30px;
    height: 30px;
  }
  
  .carousel-control i {
    font-size: 16px;
  }
  
  .carousel-control.prev {
    left: 10px;
  }
  
  .carousel-control.next {
    right: 10px;
  }
}
.product-container {
  min-height: 100vh;
  background: #f8f9fa;
}

.top-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: white;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  z-index: 1000;
}

.nav-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 15px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo h1 {
  font-size: 24px;
  color: #2E7D32;
  margin: 0;
}

.nav-links {
  display: flex;
  gap: 32px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.nav-item:hover {
  background: rgba(76, 175, 80, 0.1);
  color: #2E7D32;
}

.main-content {
  max-width: 1400px;
  margin: 80px auto 0;
  padding: 24px;
}

.header {
  background: white;
  padding: 20px;
  border-radius: 16px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.06);
  margin-bottom: 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nav-menu {
  display: flex;
  gap: 16px;
}

.menu-item {
  padding: 10px 20px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.menu-item.active {
  background: #4CAF50;
  color: white;
}

.search-box {
  display: flex;
  align-items: center;
  background: white;
  border: 2px solid #e9ecef;
  border-radius: 12px;
  overflow: hidden;
}

.search-box input {
  width: 300px;
  padding: 12px 20px;
  border: none;
  font-size: 15px;
}

.search-btn {
  padding: 12px 24px;
  background: #4CAF50;
  color: white;
  border: none;
  cursor: pointer;
  transition: all 0.3s ease;
}

.search-btn:hover {
  background: #2E7D32;
}
/* 顶部导航栏优化样式 */
.top-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: linear-gradient(90deg, #2E7D32 0%, #4CAF50 100%);
  box-shadow: 0 4px 20px rgba(46, 125, 50, 0.2);
  z-index: 1000;
  transition: all 0.3s ease;
}

.nav-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
  
  h1 {
    font-size: 28px;
    color: white;
    margin: 0;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    font-family: 'Microsoft YaHei', sans-serif;
  }

  .logo-icon {
    width: 40px;
    height: 40px;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
  }
}

.nav-links {
  display: flex;
  gap: 40px;
}

.nav-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  color: rgba(255, 255, 255, 0.9);
  text-decoration: none;
  transition: all 0.3s ease;
  font-weight: 500;

  i {
    font-size: 20px;
  }

  .nav-underline {
    position: absolute;
    bottom: 0;
    left: 50%;
    width: 0;
    height: 2px;
    background: white;
    transition: all 0.3s ease;
  }

  &:hover {
    color: white;
    transform: translateY(-2px);

    .nav-underline {
      width: 100%;
      left: 0;
    }
  }

  &.router-link-exact-active {
    color: white;
    text-shadow: 0 0 8px rgba(255,255,255,0.3);
    
    .nav-underline {
      width: 100%;
      left: 0;
    }
  }
}


$primary: #2E7D32;
$secondary: #4CAF50;
$accent: #FFD600;
$text-light: rgba(255,255,255,0.9);

.top-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 80px;
  background: linear-gradient(135deg, $primary 0%, $secondary 100%);
  box-shadow: 0 4px 30px rgba($primary, 0.3);
  z-index: 1000;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  
  &.nav-hidden {
    transform: translateY(-100%);
    box-shadow: none;
  }

  .nav-content {
    max-width: 1400px;
    height: 100%;
    margin: 0 auto;
    padding: 0 40px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: relative;
  }

  .logo {
    display: flex;
    align-items: center;
    gap: 16px;
    cursor: pointer;
    position: relative;
    transition: transform 0.3s ease;

    &:hover {
      transform: scale(1.02);
      
      .logo-glow {
        opacity: 0.8;
      }
    }

    .logo-glow {
      position: absolute;
      width: 150%;
      height: 150%;
      background: radial-gradient(circle at center, 
        rgba($accent, 0.2) 0%, 
        transparent 60%);
      opacity: 0;
      transition: opacity 0.3s ease;
    }

    .logo-icon {
      width: 48px;
      height: 48px;
      filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
      z-index: 1;
    }

    h1 {
      display: flex;
      flex-direction: column;
      line-height: 1.2;

      .logo-text {
        font-size: 28px;
        color: white;
        font-weight: 700;
        text-shadow: 0 2px 8px rgba(0,0,0,0.2);
      }

      .logo-sub {
        font-size: 12px;
        color: rgba(255,255,255,0.8);
        letter-spacing: 1px;
      }
    }
  }

  .nav-links {
    display: flex;
    gap: 30px;

    .nav-item {
      position: relative;
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 12px 20px;
      color: $text-light;
      text-decoration: none;
      overflow: hidden;
      transition: all 0.3s ease;
      border-radius: 8px;

      .nav-bg {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: linear-gradient(45deg, 
          rgba($accent, 0.1) 0%,
          rgba($accent, 0.05) 100%);
        opacity: 0;
        transition: opacity 0.3s ease;
      }

      i {
        font-size: 20px;
        transition: transform 0.3s ease;
      }

      .nav-label {
        position: relative;
        z-index: 1;
      }

      .nav-underline {
        position: absolute;
        bottom: 0;
        left: 50%;
        width: 0;
        height: 2px;
        background: $accent;
        transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
      }

      &:hover,
      &.active {
        color: white;

        .nav-bg {
          opacity: 1;
        }

        i {
          transform: translateY(-3px);
        }

        .nav-underline {
          width: 100%;
          left: 0;
        }
      }

      &.active {
        .nav-underline {
          height: 3px;
          background: linear-gradient(90deg, $accent 0%, lighten($accent, 15%) 100%);
        }
      }
    }
  }

  .nav-shine {
    position: absolute;
    bottom: 0;
    left: -50%;
    width: 200%;
    height: 2px;
    background: linear-gradient(90deg,
      transparent 20%,
      rgba(255,255,255,0.3) 50%,
      transparent 80%);
    animation: navShine 5s infinite linear;
  }

  @keyframes navShine {
    0% { transform: translateX(-50%); }
    100% { transform: translateX(100%); }
  }

  /* 移动端样式 */
  .hamburger {
    display: none;
    width: 32px;
    height: 32px;
    position: relative;
    cursor: pointer;
    z-index: 1001;

    .hamburger-line {
      position: absolute;
      width: 100%;
      height: 3px;
      background: white;
      transition: all 0.3s ease;
      
      &:nth-child(1) { top: 6px; }
      &:nth-child(2) { top: 14px; }
      &:nth-child(3) { top: 22px; }

      &.open {
        &:nth-child(1) { transform: rotate(45deg) translate(5px, 8px); }
        &:nth-child(2) { opacity: 0; }
        &:nth-child(3) { transform: rotate(-45deg) translate(5px, -8px); }
      }
    }
  }

  .mobile-nav {
    position: absolute;
    top: 80px;
    left: 0;
    right: 0;
    background: rgba($primary, 0.98);
    padding: 20px;
    display: none;
    flex-direction: column;
    gap: 15px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);

    &-enter-active,
    &-leave-active {
      transition: all 0.3s ease;
    }

    &-enter-from,
    &-leave-to {
      opacity: 0;
      transform: translateY(-20px);
    }

    .mobile-nav-item {
      color: white;
      padding: 12px;
      border-radius: 6px;
      text-decoration: none;
      transition: background 0.3s ease;

      &:hover {
        background: rgba(white, 0.1);
      }
    }
  }

  @media (max-width: 992px) {
    height: 70px;

    .nav-content {
      padding: 0 20px;
    }

    .nav-links {
      display: none;
    }

    .hamburger {
      display: block;
    }

    .mobile-nav {
      display: flex;
    }

    .logo {
      .logo-text {
        font-size: 24px;
      }
      
      .logo-icon {
        width: 40px;
        height: 40px;
      }
    }
  }
}
@media (max-width: 768px) {
  .nav-links {
    gap: 20px;
  }
  
  .logo h1 {
    font-size: 20px;
  }
  
  .nav-item {
    padding: 8px 12px;
    font-size: 14px;
  }
}
.product-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
  padding: 10px;
}

.product-item {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}

.product-image-wrapper {
  position: relative;
  padding-top: 100%;
}

.product-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-hover {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.3);
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: all 0.3s ease;
}

.product-item:hover .product-hover {
  opacity: 1;
}

.product-info {
  padding: 20px;
}

.product-name {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
  color: #343a40;
}

.product-desc {
  font-size: 14px;
  color: #6c757d;
  margin-bottom: 16px;
  line-height: 1.5;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.product-price {
  color: #2E7D32;
  font-weight: bold;
}

.price-value {
  font-size: 20px;
}

.product-stats {
  display: flex;
  gap: 12px;
  color: #6c757d;
  font-size: 13px;
}

@media (max-width: 1200px) {
  .main-content {
    padding: 20px;
  }
  
  .product-list {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
}
</style>

<script setup>

import { ref, onMounted } from "vue";
import { getGoods } from '../api/good';
import { useRouter } from "vue-router";

// 引入 Element Plus 组件
import { ElDialog, ElForm, ElFormItem, ElInput, ElButton, ElMessage , ElMessageBox, ElCarousel, ElCarouselItem } from 'element-plus';

const router = useRouter()
const products = ref([]);
const allProducts = ref([]); // 存储所有商品数据
const searchKeyword = ref(''); // 存储搜索关键词

// 定义轮播图数据
const carouselImages = ref([
  { imageUrl: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.OfKcaNLHVdQCsb2pHK2rZQAAAA?w=203&h=203&c=7&r=0&o=5&dpr=1.4&pid=1.7' },
  { imageUrl: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.k9XqANJYFjbh6Z87XORSPwHaHa?w=195&h=195&c=7&r=0&o=5&dpr=1.4&pid=1.7' },
  { imageUrl: 'https://tse1-mm.cn.bing.net/th/id/OIP-C.r74FENs-5-ox_QUDjYgKWwHaFj?w=260&h=195&c=7&r=0&o=5&dpr=1.4&pid=1.7' }
]);

onMounted(() => {
  const  token =localStorage.getItem("accessToken")
  if (!token) {
    // 使用 ElMessageBox 替换 alert
    ElMessageBox.alert('请先登录', '提示', {
      confirmButtonText: '确定',
      type: 'warning',
      showClose: false,
      customClass: 'exaggerated-alert',
      callback: () => {
        router.push("/");
      }
    });
  } else {
    getGoodsa(); // 页面加载时获取商品数据
  }
});

/**
 * 跳转详情页面
 */
const jumpDetail = (id) => {
  console.log(id)
  localStorage.setItem("goodsId",id)
  router.push("/orderDetail")
};

const getGoodsa1 = async () => {
  try {
    const response = await getGoods.getGoodsApi();
    console.log('成功', response);
    products.value = response.data.data.list;
    allProducts.value = response.data.data.list; // 存储所有商品数据
  } catch (error) {
    console.error('失败:', error);
  }
};

// 搜索商品方法
const getGoodsa = async () => {
  
  try {
    // 直接传递参数对象
    const response = await getGoods.getGoodsApi({
   
      goodsName: searchKeyword.value,
    
    });
    console.log('成功', response);
    products.value = response.data.data.list;
    allProducts.value = response.data.data.list; // 存储所有商品数据
  } catch (error) {
    console.error('失败:', error);
  }
};



// 添加导航项数据
const navItems = [
  { icon: 'icon-order', label: '订单' },
  { icon: 'icon-favorite', label: '我的收藏' },
  { icon: 'icon-goods', label: '我的商品' },
  { icon: 'icon-shop', label: '商品' },
  { icon: 'icon-user', label: '个人中心' }
];

// 添加分类数据
const categories = [
  { icon: 'icon-food', name: '零食' },
  { icon: 'icon-appliance', name: '家电' },
  { icon: 'icon-clothes', name: '衣服' },
  { icon: 'icon-more', name: '其他' }
];
</script>

